/**
 * Created by iallai on 2/1/2018.
 */

import axios from 'axios'
import {CONFIG} from '@/config/env'

let API=CONFIG.PROJECT.API;

const query = (query = {page: 1, size: 10}) => new Promise((resolve, reject) => {
  //

  axios.get(`${API.URL}/audit_schemas`,{
    params:query
  }).then(function(res){
      if(API.isOK(res.data)){
        resolve(res.data);
      }
  })



});
const getIds = (query={id:-1})=>new Promise((resolve, reject) => {

  console.log(query)
  axios.get(`${API.URL}/audit_schemas/${query.id}`,{
    params:query
  }).then(function(res){
      if(API.isOK(res.data)){
        resolve(res.data);
      }
    })
    .catch(function(err){
      reject("网络错误");
    });


});
const get = (query={id:-1})=>new Promise((resolve, reject) => {

  query.schema_id=query.id;
  axios.get(`${API.URL}/schema/detail`,{
    params:query
  }).then(function(res){
      if(API.isOK(res.data)){
        resolve(res.data);
      }
    })
    .catch(function(err){
      reject("网络错误");
    });


});
const audit = (data)=>new Promise((resolve, reject) => {


  axios({
    method: 'post',
    url: `${API.URL}/audit_schemas/${data.id}`,
    params:data,

  }).then(function(res){
    if(API.isOK(res.data)){
      resolve(res.data);
    }else{
      reject(res.data.message);
    }
  }).catch(function(err){
    reject("网络错误");
  });


});

const verify= (data)=>new Promise((resolve, reject) => {

  data.for_validate=1;
  axios({
    method: 'post',
    url: `${API.URL}/schema/upsert`,
    params:data,

  }).then(function(res){
    if(API.isOK(res.data)){
         resolve(res.data);
    }
  }).catch(function(err){
        reject("网络错误");
    });



});

const agree= (data)=>new Promise((resolve, reject) => {

  data.status="pass";
  axios({
    method: 'post',
    url: `${API.URL}/dream_car`,
    params:data,

  }).then(function(res){
    if(API.isOK(res.data)){
      resolve(res.data);
    }
  }).catch(function(err){
    reject("网络错误");
  });



});
const update= (data)=>new Promise((resolve, reject) => {

  data.for_validate=0;
  axios({
    method: 'post',
    url: `${API.URL}/schema/upsert`,
    params:data,

  }).then(function(res){
    if(API.isOK(res.data)){
      resolve(res.data);
    }
  }).catch(function(err){
    reject("网络错误");
  });



});



export {query,get,getIds,verify,update,audit,agree};

